<template>
  <!-- 新增清单或超额合同审批 -->
  <base-breadcrumb :title="$route.meta.title">
    <!-- 搜索 -->
    <searchData :searchData="searchArr" @search="search"></searchData>
    <!-- 表格 -->
    <a-card :bordered="false">
      <a-button class="mb12" icon="plus" type="primary" @click="handleManage(2)" v-btnPermission="'bm_024_add_01'">新增</a-button>
      <a-alert class="mb12" type="info" show-icon>
        <template slot="message">
          <span>
            原合同额(万元)：
            <a>{{ statistics.originalContractMoneyAll }}</a>
          </span>
          <a-divider type="vertical" />
          <span>
            累计增加金额(万元)：
            <a>{{ statistics.increaseMoneyAll }}</a>
          </span>
          <a-divider type="vertical" />
          <span>
            现合同额(万元)：
            <a>{{ statistics.currentContractMoneyAll }}</a>
          </span>
        </template>
      </a-alert>
      <!-- 表格 -->
      <list-table ref="table"  :page-keys="['pageNum', 'pageSize','totalSize']" tableLayout="fixed" size="default" rowKey="id" :columns="columns" :request="loadData" :alert="true" show-size-changer show-quick-jumper showPagination="auto">
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a @click="handleManage(0, record)" v-btnPermission="'bm_024_loo_04'">查看</a>
            <span v-if="record.auditStatus != 'SUBMITTED' && record.auditStatus != 'PAST' && record.auditStatus != 'ABOLISH'" v-btnPermission="'bm_024_edi_02'">
              <a @click="handleManage(1, record)">编辑</a>
            </span>

            <span v-if="record.auditStatus == 'SUBMITTED' && userId === record.creatorId&&hideBuquBtn()">
              <a @click="handleUndo({ id: record.id })">撤销</a>
            </span>
            <span v-if="record.auditStatus == 'PAST'" v-btnPermission="'bm_024_tov_05'">
              <a @click="handleInvalid({ id: record.id })">作废</a>
            </span>
            <!-- 删除 -->
            <span v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'" v-btnPermission="'bm_024_del_03'">
              <a @click="handleRemoveParams({ id: record.id })">删除</a>
            </span>

            <span v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'ABOLISH'" v-btnPermission="'bm_024_loo_04'">
              <approval-button :id="record.id" :status="record.auditStatus" />
            </span>
          </MoreButton>
        </template>
        <template slot="auditStatus" slot-scope="text, record">
          <list-status :status="record.auditStatus" conversion="en" :is-approval="true" :text="text" />
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { getAddListContractList, delAddListContract, revokeAddListContract, undoAddListContract, getAddListContractStatistical } from '@/api/businessAffairs/addListContract' // api
import BasePage from '@/views/basePage' // 基础配置
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import MoreButton from '@/components/MoreButton'
import { typeLabel, status, typeListApproval } from './model.js'
// import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商单位
import SelectSubcontractorCode from '@/components/Select/SelectSubcontractorCode' // 分包商
const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchCompanyId',
  //   type: SelectSubCompany
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchCompanyId',
  //   valueKey: 'projectId',
  //   type: SelectProject
  // },
  {name: '分包单位',type: 'input',value: 'subcontractorName'},
  {
    name: '合同名称',
    type: 'input',
    value: 'contractName'
  },
  {
    name: '申请人',
    type: 'input',
    value: 'applicantName'
  },
  {
    name: '类别',
    value: 'type',
    type: 'select',
    data: typeLabel
  },
  {
    name: '填报时间',
    type: 'dateRange',
    value: 'activityDate',
    attr: {
      rangeFormat: ['updateStartTime', 'updateEndTime']
    }
  },
  {
    name: '状态',
    value: 'auditStatus',
    type: 'select',
    data: typeListApproval
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '所属分公司',
    dataIndex: 'branchCompanyName',
    ellipsis: true
  },

  {
    title: '所属项目',
    dataIndex: 'projectName',
    ellipsis: true
  },
  {
    title: '分包单位',
    dataIndex: 'subcontractorName',
    ellipsis: true
  },
  {
    title: '合同名称',
    dataIndex: 'contractName',
    ellipsis: true
  },
  {
    title: '类别',
    dataIndex: 'type',
    customRender() {
      let val = arguments[0]
      let obj = typeLabel.find((v) => v.value == val)
      return obj?.label || ''
    }
  },
  {
    title: '本次增加金额(万元)',
    dataIndex: 'increaseMoney'
  },
  {
    title: '现合同额(万元)',
    dataIndex: 'currentContractMoney'
  },
  {
    title: '申请人',
    dataIndex: 'applicantName',
    ellipsis: true
  },
  {
    title: '填报时间',
    dataIndex: 'updateTime'
  },
  {
    title: '状态',
    dataIndex: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    // align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    MoreButton
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      userId: JSON.parse(sessionStorage.getItem('ycloud-user_info')).id,
      statistics: { currentContractMoneyAll: 0, increaseMoneyAll: 0, originalContractMoneyAll: 0 }, //统计
      loadData: async (parameter) => {
        const requestParameters = Object.assign({}, parameter, { param: this.queryParam })
        this.getAddListContractStatisticalReq(this.queryParam)
        return getAddListContractList(requestParameters).then((res) => {
          if (res.code === '0') {
            res.data.list.forEach((i) => {
              i.auditStatus = status[i.auditStatus]
            })
            return {
              code: 200,
              data: res.data.list,
              totalSize: res.data?.total,
              currentPage: res.data?.pageNum
            }
          }
        })
      },
      removeApi: { remove: delAddListContract, invalid: revokeAddListContract, undo: undoAddListContract } // api
    }
  },
  methods: {
    getAddListContractStatisticalReq(requestParameters) {
      getAddListContractStatistical(requestParameters).then((res) => {
        this.statistics = res.data
      })
    },
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updateAddListContract',
        query
      })
    }
  }
}
</script>
